<template>
  <div :class="className">
    <!-- style -->
    <div v-html="css"></div>

    <!--客服静态-->
    <div class="customer_service">
      <template v-if="datas.style_radio === '1'">
        <div class="service-item" v-for="(item, index) in datas.list" :key="index" @click="toService(item)">
          <div class="avatar">
            <img :src="item.avatar || emptyImage" alt="" />
          </div>
          <div class="name">{{ item.nickname }}</div>
          <span class="btn-session" @click.stop="toChat(item)">
            <span class="button-item">{{ datas.button_title }}</span>
          </span>
        </div>
      </template>
      <template v-if="datas.style_radio === '2'">
        <div class="lawyer" v-for="(item, index) in datas.list" :key="index" @click="toService(item)">
          <div class="d-f ">
            <div class="d-f y-l">
              <img :src="item.avatar || emptyImage" alt="" />
            </div>
            <div class="d-f y-r">
              <div class="d-f f-j-c">
                <div class="s-16 name_color" style="">{{ item.nickname || "客服昵称" }}</div>
                <div class="btn s-10" @click.stop="toChat(item)">{{ datas.button_title }}</div>
              </div>
              <div class="d-f" style="flex-wrap: wrap;" v-if="item.tags">
                <div class="job_color tag_1" v-for="(str, i) in item.tags.split(/\s+/)" :key="i">{{ str }}</div>
              </div>
              <div class="d-f" style="line-height: 12px;">
                <div class="tag s-10">{{ item.sketch || "---" }}</div>
              </div>
            </div>
          </div>
        </div>
      </template>

      <div style="font-size: 20px; font-weight: bold; margin: 15px auto;" v-if="!datas.list || datas.list.length <= 0">
        暂无添加客服
      </div>
    </div>
  </div>
</template>

<script>
import emptyImage from "@/assets/images/new_diy/image.png";

// 自定义样式
const css = function() {
  if (this.datas.preview_color) {
    const { preview_color, font_size, font_color, button_size, button_bg_color, button_color, show_border, button_border_color,job_color ,job_bg } = this.datas;

    return `
      .component-${this.id} .customer_service {
          background-color: ${preview_color.color || "#ffffff"};
          background-image: url(${preview_color.isColor == 2 ? preview_color.image : ""});
          background-size: 100% 100%;
          background-position: center;
          background-repeat: no-repeat;
      }

      .component-${this.id} .customer_service .name {
          font-size: ${font_size}px;
          color: ${font_color};
      }

      .component-${this.id} .customer_service .btn-session {
          background-color: ${button_bg_color};
          font-size: ${button_size}px;
          color: ${button_color};
          border: solid ${show_border ? "1" : "0"}px ${button_border_color};
      }

      .component-${this.id} .lawyer .job_color {
            color: ${job_color};
            background-color: ${job_bg};
            border-radius: 3px;
        }

      .component-${this.id} .lawyer .name_color {
        font-size: ${font_size}px;
        color: ${font_color};
      }
      .component-${this.id} .lawyer .btn {
          color: ${button_color};
          font-size: ${button_size}px;
          background-color: ${button_bg_color};
          border: solid ${show_border ? "1" : "0"}px ${button_border_color};
      }

  `;
  }
};

export default {
  data() {
    return {
      emptyImage
    };
  },
  props: ["id", "datas", "styles"],
  computed: {
    css() {
      return "<style>" + css.call(this) + "</style>";
    },
    className() {
      const name = ["component-wrapper", `component-${this.id}`];
      return name;
    }
  },

  methods: {
    toService(item) {
      this.$router.push(this.fun.getUrl("personalProfile", {}, { id: item.id }));
    },
    toChat(item) {
      window.location.href = item.h5_url;
    }
  },

  mounted() {
    this.$emit("loaded");
  }
};
</script>

<style lang="scss" scoped>
.customer_service {
  display: flex;
  flex-flow: wrap;
  padding: 10px 8px 0;
  font-size: 12px;
  color: #333;

  .service-item {
    display: flex;
    flex: 0 0 30%;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;
    padding: 10px 5px;
    background-color: #fff;
    border-radius: 10px;
    margin: 0 4px 10px;
  }

  .avatar {
    width: 100px;
    height: 100px;
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 10px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .btn-session {
    color: #3b98f7;
    margin-top: 10px;
    padding: 2px 8px;
    border-radius: 15px;
    background-color: #f5f5f5;
  }
}

.lawyer {
  margin: 0 6px 14px 6px;
  box-shadow: 1px 2px 4px rgba(61, 61, 61, 0.25);
  border-radius: 10px;
  padding: 10px;
  font-size: 14px;
  width: 100%;
  background: #fff;

  .d-f {
    display: flex;
    align-items: center;
  }

  .f-j-c {
    justify-content: space-between;
  }

  .y-l {
    margin-right: 12px;

    img {
      width: 87px;
      height: 87px;
      border-radius: 4px;
      overflow: hidden;
      object-fit: cover;
    }
  }

  .y-r {
    flex: 1;
    flex-direction: column;
    height: 87px;
    justify-content: space-between;

    .d-f {
      width: 100%;
    }
  }

  .s-16 {
    font-size: 16px;
  }

  .s-10 {
    font-size: 10px;
  }

  .tag {
    text-align: left;
    line-height: 16px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .tag_1 {
    padding: 0 5px;
    line-height: 18px;
    margin-right: 5px;
    font-size: 12px;
    margin-bottom: 4px;
  }

  .btn {
    padding: 0 6px;
    text-align: center;
    border: 1px solid #1b1b1d;
    border-radius: 10px;
  }
}
</style>
